<?php

$symb = $this->countSimbols;
$type=1;
switch ($symb){
case "151":
     $type = 1;
	 	echo "<div class=\"vitr_2\">";

	 break;

default:$type=2;break;
}


$posts_per_page = $this->rowCount * $this->colCount;

if (isset($this->category)){$selection = "cat={$this->category}";
}else{$selection = "tag={$this->tag}";}

if($type == 1){
echo "<div id=\"fp-vitrina-head\"><h2>Продукты</h2></div>"
     ."<div class=\"justifyli\">";
      //."<!--<ul class=\"liap\">-->"
     }
  if($type == 2){echo "<table style=\"width:100%\">";}
  
$query = new WP_Query("{$selection}&showposts={$posts_per_page}&paged={$this->page}");
$counter = 0;

while($query->have_posts()){
	
    $query->the_post();
    	$image = get_post_meta($query->post->ID,'pic',true);
    	$short_text = get_post_meta($query->post->ID,'short_text',true);
        $buy_block = $this->shop->GetGoodWidget($query->post,null,array('name'=>true,'cost'=>true,'add'=>true));
        $title = $query->post->post_title;
        $gid = $query->post->ID;
        $link = get_permalink( $query->post->ID );


switch($type){

case 1:
$output = preg_match_all('/<td\s+class\s*?=\s*?[\'"]wpshop_price[\'"]\s*?>\s*(\d*\s*)/i', $buy_block, $matches);
$pricestr = $matches[1][0]."руб.";

    $nach = strpos($buy_block,'<a href="#" onclick="addtocart(',0)+ 31;
    $kon = strpos($buy_block,');',$nach);
    $addlink = trim(substr($buy_block,$nach, $kon-$nach));
    $addlink = "<a href=\"#\" onclick=\"addtocart(".$addlink."); return false;\">";


    if (($counter % $this->colCount) == 0) echo "<div class=\"vcont_2\">";


echo "<div class=\"vart_2\">";

echo "<div class=\"gdbx_2\">";

echo  "<div class='vimg'>{$image}</div>";

echo "<span class=\"sgcnt_2\"><a href='".$link."' class=\"sgn_2\">".$title."</a></span>";


echo "<div class=\"dlbl_2\"><span class=\"la_2\"></span>"

               ."<span class=\"sprs_2\">".$pricestr."</span>"
               ."<span  class=\"kav_2 rf_2\" >&raquo;</span>"
               ."<input type=\"hidden\" name=\"wpshop-good-title-".$gid."\" value=\"".$title."\">"
               ."<span class=\"ra_2\"></span>"
               ."<span class=\"mkrz_2\">".$addlink
               ."<img src=\"http://maralpant.ru/wp-content/themes/realmaral/images/krz.png\" style='display:inline;'/>"
               ."</a></span>"
               ."<span class=\"kav_2 lf_2\">&laquo;</span>"
               ."</div>";

             echo "</div>";

    echo "</div>&nbsp;";//  &nbsp;"; не убирать \n!!!!! Без этого не работатет

    	$counter++;
    	if (($counter % $this->colCount) == 0) echo "<div class=\"ftr_2\"></div></div>";
                   
         break;


	case 2:

	$short_text = get_post_meta($query->post->ID,'short_text',true);
	if (empty($short_text))
	{
		$short_text = "".mb_substr(strip_tags($query->post->post_content),0,$this->countSimbols)."...";
	}
	$buy_block = $this->shop->GetGoodWidget($query->post,null,array('name'=>true,'cost'=>true,'add'=>true));
	$detail = "<a href='" . get_permalink( $query->post->ID ) . "'>Подробнее</a>";

	if (($counter % $this->colCount) == 0) echo "<tr valign='top'>";
	echo "<td style='padding:0px;width:260px;'>";
	echo "<div class='vitrina_element' style='height:{$this->height}px;'>";
	echo "<div class='vitrina_header'><a href='".get_permalink( $query->post->ID )."'>".$query->post->post_title."</a></div>";
	echo "<div class='vitrina_image'>{$image}</div>";
	echo "<div class='shopwindow_content'>{$short_text}</div>";
	echo "<div class='vitrina_detail shopwindow_content' style='text-align:right'>{$detail}</div>";
	echo "<div align='center' style='margin-top:20px;'>{$buy_block}</div>";
	echo "</div>";
	echo "</td>";
	$counter++;
	if (($counter % $this->colCount) == 0) echo "</tr>";
break;
}
}

wp_reset_query();



switch ($type){
case 1:
if($counter % $this->colCount==0){echo "</div></div>";}else{echo "<div class=\"ftr_2\"></div></div></div></div>";}
  break;

case 2:
$was = false;
while(($counter % $this->colCount) != 0)
{
	echo "<td style='width:1%'></td>";
	$was = true;
	$counter++;
}
if ($was) echo"</tr>";

echo "</table>";
break;
 }
      /*
if ( $posts_per_page > 0 ) :
	$pages = array();

	for ($i = 0; $i < $query->found_posts/$posts_per_page; $i++){
		if ($this->page == $i+1){$pages[] = ($i+1);}
		else
		{$uri = $_SERVER["REQUEST_URI"];
			if (strpos($uri,'?') === false){$separate = "?";}
			else
			{$separate = "&";}
        	if (strpos($uri,'vpage') === false){
				$url = "{$uri}{$separate}vpage=".($i+1);
			}else{$url = preg_replace("/vpage=(\d+)/","vpage=".($i+1),$uri);}
			$pages[] = "<a href='{$url}'>".($i+1)."</a>";
		}
	}
	echo "<div align='center'>" . join(' | ',$pages) . "</div>";
  endif;
        */